<?php 
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Book extends Controller
{
	/**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = db('data as d, s75_category as c')->field('c.name cname, d.id, cid, d.name, price, author, face')
                ->where('cid = c.id')->select();

        return view('book/list', ['title'=>'图书列表','data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $list = $this->showAll();

        return view('book/add', ['title'=>'添加图书','list'=>$list]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {   
         // 新增数据
        $data = $request->post();

        // 获取表单上传文件
        $file = request()->file('face');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->validate(['size'=>555555,'ext'=>'jpg,png,gif'])->move(ROOT_PATH .'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                // echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // echo $info->getFilename();
                $data['face'] = '/uploads/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                 echo $file->getError();
            }
        }else{$data['face'] = '';}

        $res = db('data')->insert($data);
        if($res){ return $this->success('添加成功',url('admin/Book/index')); }else{ return $this->error('添加失败'); }
        
        
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    { 
		$data = $this->getAll($id);

        return view('book/detail', ['title'=>'图书详情', 'data'=>$data]);
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
    	$data = $this->getAll($id);
        $list = $this->showAll();

        return view('book/edit', ['title'=>'编辑图书','data'=>$data, 'list'=>$list]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {   
        $data = $request->put();
        $data =array_diff_key($data, ['_method' => "Put"]);

        // 获取表单上传文件
        $file = request()->file('face');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->validate(['size'=>555555,'ext'=>'jpg,png,gif'])->move(ROOT_PATH .'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                // echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // echo $info->getFilename();
                $data['face'] = '/uploads/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                 echo $file->getError();
            }
        }

        $res = db('data')->update($data);

        if($res){ return $this->success('修改成功',url('admin/Book/index')); }else{ return $this->error('修改失败'); }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        $result = db('data')->delete($id);

        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的图书删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }

        // echo json_encode($result);
        return json($info);
    }


    // 遍历所有分类
    public function showAll()
    {
        $list = db('category')->field(['id','name'])->where('pid','=','0')->select();
        $res = db('category')->field(['id','pid','name'])->where('pid','>','0')->select();

        foreach ($list as $k => $v) {
            $list[$k]['list'] = [];
            foreach ($res as $ko => $vo) {
                if ($v['id']==$vo['pid']) {
                    $vo = array_diff_key($vo,['pid'=>'1']);
                    $list[$k]['list'][] = $vo;
                }
            }
        }
        return $list;
    }

    // 获取一条数据的全部信息
    public function getAll($id)
    {
      $data = db('data as d, s75_category as c')
        ->field('c.name cname, d.id, cid, d.name, price, author, face, tag, copyright, ctime, hot, search, score, comment, aid')->where('cid = c.id')->where('d.id',$id)->find();
        return $data;
    }

}


 ?>